package netctoss.demo.controller;

import com.github.pagehelper.PageInfo;
import netctoss.demo.entrty.Account;
import netctoss.demo.entrty.Result;
import netctoss.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@RestController
@RequestMapping("manager/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("/account/account_list.html");
    }
    @RequestMapping("showDetail")
    public ModelAndView showDetail(){
        return new ModelAndView("/account/account_detail.html");
    }
    @RequestMapping("toUpdate")
    public ModelAndView showUpdate(){
        return new ModelAndView("/account/account_modi.html");
    }
    @RequestMapping("toInsert")
    public ModelAndView toInsert(){
        return new ModelAndView("/account/account_add.html");
    }

    @RequestMapping("doInsert")
    public Result doInsert(@RequestBody Account account){
        account.setAccountCreateDate(new Date());
        account.setAccountStatus("0");
        int i = accountService.insertSelective(account);
        return new Result(true,"添加成功",i);
    }
    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody Account account){
        int i = accountService.updateByPrimaryKeySelective(account);
        return new Result(true,"修改成功",i);
    }

    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize, Account account){
        PageInfo<Account> pageInfo = accountService.selectPage(pageNum,pageSize,account);
        return new Result(true,"查询成功",pageInfo);
    }


    @RequestMapping("selectOne/{aid}")
    public Result selectOne(@PathVariable("aid") Long aid){
//        Account account = accountService.selectByPrimaryKey(aid);
        return new Result(true,"查询成功",accountService.selectByAid(aid));
    }



    @RequestMapping("doPause/{aid}") //暂停
    public Result doPause(@PathVariable("aid") Long aid){
        Account account = new Account();
        account.setAccountPauseDate(new Date());
        account.setAccountStatus("1");
        account.setAccountId(aid);
        return new Result(true,"修改成功",accountService.updateByPrimaryKeySelective(account));
    }

    @RequestMapping("doDelete/{aid}") //暂停
    public Result doDelete(@PathVariable("aid") Long aid){
        Account account = new Account();
        account.setAccountCloseDate(new Date());
        account.setAccountStatus("2");
        account.setAccountId(aid);
        return new Result(true,"修改成功",accountService.updateByPrimaryKeySelective(account));
    }

    @RequestMapping("selectByCardNo")
    public Result selectByCardNo( String cardNo){
        Account account = new Account();
        account.setAccountIdcardNo(cardNo);
        Account one = accountService.selectOne(account);
        if (one != null){
            return new Result(true,"1",one);
        }else {
            return new Result(true,"0",null);
        }

    }

}
